package com.android.miniskirt.ui;

import com.android.miniskirt.miniApplication;
import com.android.miniskirt.service.MQTTService;

import android.app.NotificationManager;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;  
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;  
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.widget.TabHost;  
import android.widget.TabWidget;
import android.widget.TextView;
   
public class minitab extends TabActivity{  
  
	
	private StatusUpdateReceiver statusUpdateIntentReceiver;
	private MQTTMessageReceiver  messageIntentReceiver;
	private miniApplication mApplication;
     /* (non-Javadoc)
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	    unregisterReceiver(statusUpdateIntentReceiver);
	    unregisterReceiver(messageIntentReceiver);  

	}
	public TabHost tabHost;  
   
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);
         
         TabHost tabHost = getTabHost();
         final TabWidget tabWidget = tabHost.getTabWidget();
         TextView txtTabInfo = new TextView(this);
         txtTabInfo.setText("联系人");
         txtTabInfo.setPadding(0, 5, 0, 0);
         txtTabInfo.setTextSize(11);

//         txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_active_right_inactive);
         txtTabInfo.setTextColor(Color.TRANSPARENT);
         txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL);
         txtTabInfo.setHeight(39);

         int height = 40;
         int width = 50;
         tabHost.addTab(tabHost.newTabSpec("tab1")   
        		                  .setIndicator("联系人")   
        		                  .setContent(new Intent(this, FirstGroup.class)));
         
         tabHost.addTab(tabHost.newTabSpec("tab2")   
                 .setIndicator("最近联系人")   
                 .setContent(new Intent(this, FirstGroup.class)));
         
         tabHost.addTab(tabHost.newTabSpec("tab3")   
                 .setIndicator("分组联系")   
                 .setContent(new Intent(this, FirstGroup.class)));
         tabHost.setCurrentTab(0);
         
         for (int i =0; i < tabWidget.getChildCount(); i++) {

             //鐠佸墽鐤唗ab閻ㄥ嫰鐝惔锔猴拷鐎硅棄瀹�

             tabWidget.getChildAt(i).getLayoutParams().height = height;

             tabWidget.getChildAt(i).getLayoutParams().width = width;
             
//             tabWidget.getChildAt(i).setBackgroundColor();

    }
     }
     
     public class StatusUpdateReceiver extends BroadcastReceiver
     {
         @Override
         public void onReceive(Context context, Intent intent)
         {
             Bundle notificationData = intent.getExtras();
             String newStatus = notificationData.getString(MQTTService.MQTT_STATUS_MSG);	    	  

          
         }
     }
     public class MQTTMessageReceiver extends BroadcastReceiver
     {
         @Override
         public void onReceive(Context context, Intent intent)
         {
             Bundle notificationData = intent.getExtras();
             String newTopic = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_TOPIC);
             String newData  = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_MSG);	    	  
             
             mApplication.mIsNewMessage = true;
             mApplication.mMessage 		= newData;
             mApplication.mTopic 		= newTopic;
             
         }
     }
     
     public void Register() {
    	 
     	 	TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     	 	String tel = tm.getLine1Number();
     	 	SharedPreferences settings = getSharedPreferences(MQTTService.APP_ID, 0);
     	 	SharedPreferences.Editor editor = settings.edit();
     	 	editor.putString("broker","mobileloc.ddns.mobi");
     	 	editor.putString("topic",tel);
     	 	editor.commit();
     	 	
    	    statusUpdateIntentReceiver = new StatusUpdateReceiver();
    	    IntentFilter intentSFilter = new IntentFilter(MQTTService.MQTT_STATUS_INTENT);
    	    registerReceiver(statusUpdateIntentReceiver, intentSFilter);  

    	    messageIntentReceiver = new MQTTMessageReceiver();
    	    IntentFilter intentCFilter = new IntentFilter(MQTTService.MQTT_MSG_RECEIVED_INTENT);
    	    registerReceiver(messageIntentReceiver, intentCFilter);
    	    
        	Intent svc = new Intent(this, MQTTService.class);
        	startService(svc); 
     }
     
     @Override
     public void onWindowFocusChanged(boolean hasFocus)
     {
         super.onWindowFocusChanged(hasFocus);
         if (hasFocus)
         {
             NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
             mNotificationManager.cancel(MQTTService.MQTT_NOTIFICATION_UPDATE);
         }
     } 
}